﻿using System.Collections.Generic;
using System.Linq;

namespace DynamicGeometry
{
    public class RemoveFigureAction : GeometryAction
    {
        public RemoveFigureAction(Drawing drawing, IFigure figure)
            : base(drawing)
        {
            Figure = figure;
        }

        public IFigure Figure { get; set; }
        public IFigure[] Deleted { get; set; }

        protected override void ExecuteCore()
        {
            Deleted = Figure.AsEnumerable<IFigure>()
                .TopologicalSort(f => f.Dependents)
                .ToArray();

            foreach (var figure in Deleted)
            {
                Drawing.Figures.Remove(Figure);
            }
        }

        protected override void UnExecuteCore()
        {
            foreach (var figure in Deleted.Reverse())
            {
                Drawing.Figures.Add(Figure);
            }
        }
    }
}
